<template>
    <div class="table-box">
        <div class="table-search">
            <el-form :inline="true" :model="form">
                <el-form-item label="商品ID">
                    <el-input v-model="form.goods_id" placeholder="商品ID" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button @click="onReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card content-box">
            <div class="table">
                <el-table :data="tableData" border v-loading="loading">
                    <el-table-column prop="id" label="ID" align="center" width="70" />
                    <el-table-column prop="goods_id" label="商品ID" align="center" />
                    <el-table-column prop="code" label="兑换码" align="center" width="180" />
                    <el-table-column label="是否使用" align="center">
                        <template #default="scope">
                        <el-tag :type="scope.row.is_use === 1 ? 'success' : 'warning'">
                        {{ scope.row.status === 1 ? "已使用" : "未使用" }}
                        </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createtime" label="时间" align="center" :formatter="formatterTime" width="180" />
                </el-table>
                <div class="table-pagination">
                    <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize"
                        :page-sizes="[10, 25, 50, 100]" :background="true" layout="total, sizes, prev, pager, next, jumper"
                        :total="pageable.total" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"></el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, timestamp } from "@/utils/utils";
import { blinkboxCodesApi } from "@/api/moby/blinkbox/blinkbox";

const tableData = ref([]);
const loading = ref(false);

const formData = {
    goods_id: ""
};
const form = reactive({ ...formData });

const pageable = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
});

// 参数
const param = () => {
    return {
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
        goods_id: form.goods_id,
    };
};

// 获取表格数据
const getTbaleData = async () => {
    loading.value = true;
    try {
        const res = await blinkboxCodesApi(param());
        loading.value = false;
        tableData.value = res.data.results;
        pageable.total = res.data.count;
    } catch (error) {
        loading.value = false;
    }
};
// 重置
const onReset = () => {
    Object.assign(form, formData);getTbaleData();
};
// 查询
const onQuery = async () => {
    getTbaleData();
};

// 条数
const handleSizeChange = (number) => {
    pageable.pageSize = number;
    getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
    pageable.pageNum = number;
    getTbaleData();
};
onMounted(() => {
    getTbaleData();
});
</script>
  